var gulp = require('gulp');
var concat = require('gulp-concat');
var header = require('gulp-header');
var connect = require("gulp-connect");
var less = require("gulp-less");
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var ejs = require("gulp-ejs");
var uglify = require('gulp-uglify');
var ext_replace = require('gulp-ext-replace');
var cssmin = require('gulp-cssmin');
var sourcemaps = require('gulp-sourcemaps');

var pkg = require("./package.json");

//头部版本信息
var banner =
    "/** \n\
    * note ui V" + pkg.version + " \n\
    * By LineSoft\n\
    * http://git.oschina.net/linesoft/noteui/\n \
    */\n";

gulp.task('js', function (cb) {

    count = 0;
    var end = function () {
        count++;
        if (count >= 3) cb();
    };
    //省市区
    gulp.src([
        './src/js/city-data.js',
        './src/js/city-picker.js'
    ])
        .pipe(concat({path: 'city-picker.js'}))
        .pipe(gulp.dest('./dist/js/'))
        .on("end", end);
        //滑动插件
    gulp.src([
        './src/js/swiper.jquery.js',
        './src/js/swiper-wrap.js',
        './src/js/photos.js'
    ])
        .pipe(concat({path: 'swiper.js'}))
        .pipe(gulp.dest('./dist/js/'))
        .on("end", end);

    gulp.src([
        './src/js/jquery-extend.js',
        './src/js/template7.js',
        './src/js/hammer.js',
        './src/js/modal.js',
        './src/js/toast.js',
        './src/js/action.js',
        './src/js/pull-to-refresh.js',
        './src/js/infinite.js',
        './src/js/tab.js',
        './src/js/search-bar.js',
        './src/js/device.js',
        './src/js/picker.js',
        './src/js/select.js',
        './src/js/calendar.js',
        './src/js/datetime-picker.js',
        './src/js/popup.js',
        './src/js/notification.js',
        './src/js/toptip.js'
    ])
        .pipe(concat({path: 'note-jq.js'}))
        .pipe(header(banner))
        .pipe(gulp.dest('./dist/js/'))
        .on("end", end);


});

gulp.task('uglify', ["js"], function () {
    return gulp.src(['./dist/js/*.js', '!./dist/js/*.min.js'])
        .pipe(uglify({
            preserveComments: "license"
        }))
        .pipe(ext_replace('.min.js'))
        .pipe(gulp.dest('./dist/js'));
});

gulp.task('less', function () {
    return gulp.src(['./src/less/style/style.less','./src/less/noteui.less'])
        .pipe(sourcemaps.init())
        .pipe(less().on('error', function (e) {
            console.error(e.message);
            this.emit('end');
        }))
        .pipe(postcss([autoprefixer(['iOS >= 7', 'Android >= 4.1'])]))
        .pipe(concat({path: 'note.css'}))
        .pipe(header(banner))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('./dist/css/'));

});

gulp.task('cssmin', ["less"], function () {
    gulp.src(['./dist/css/note.css', '!./dist/css/note.min.css'])
        .pipe(cssmin())
        .pipe(header(banner))
        .pipe(ext_replace('.min.css'))
        .pipe(gulp.dest('./dist/css/'));
});

gulp.task('ejs', function () {
    return gulp.src(["./src/demos/*.html", "!./src/demos/_*.html"])
        .pipe(ejs({}))
        .pipe(gulp.dest("./demos/"));
});

gulp.task('copy', function () {
    gulp.src(['./src/lib/**/*'])
        .pipe(gulp.dest('./dist/lib/'));

    gulp.src(['./src/demos/images/*.*'])
        .pipe(gulp.dest('./demos/images/'));

    gulp.src(['./src/demos/css/*.css'])
        .pipe(gulp.dest('./demos/css/'));
});

gulp.task('watch', function () {
    gulp.watch('src/js/**/*.js', ['js']);
    gulp.watch('src/less/**/*.less', ['less']);
    gulp.watch('demos/*.html', ['ejs']);
    gulp.watch('demos/css/*.css', ['copy']);
});

gulp.task('server', function () {
    connect.server({
        name: 'demos',
        root: '',
        port: 8080,
        livereload: true
    });
});
gulp.task("default", ['uglify', 'cssmin', 'copy', 'ejs','server']);
